package org.jdu.dao;



import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.sql.DataSource;

import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.log4j.Logger;
import org.jlu.logger.JluLogger;

/**
 * Consente la connessione al db attraverso i driver jdbc.
 * @author epelli
 *
 */
public class SimpleConnection extends AbstractDBConnection {
	
	private ResourceBundle bundle = ResourceBundle.getBundle("crparch");
	private Logger log = JluLogger.getLogger();

	public Connection getConnection(String dbKeyName) throws DbException{
		log.info("[SimpleConnection::getConnection] BEGIN");
		try {
			Map map = new HashMap();
			map.put("dbname", dbKeyName);
			StrSubstitutor subs = new StrSubstitutor(map);
			
			String user = getBundle().getString(subs.replace(PropertiesNames.DB_JDBC_USER));
			String url = getBundle().getString(subs.replace(PropertiesNames.DB_JDBC_URL));
			String driver = getBundle().getString(subs.replace(PropertiesNames.DB_JDBC_DRIVER));
			String pwd =  getBundle().getString(subs.replace(PropertiesNames.DB_JDBC_PASSWORD));
			
//			String ncharprop = "oracle.jdbc.defaultNChar";
//			String ncharvalue = getBundle().getString("db.nchar");
//			ncharvalue = ncharvalue==null?"false":ncharvalue;
			Properties p = new Properties();
			p.setProperty("user", user);
			p.setProperty("password", pwd);
//			p.setProperty(ncharprop, ncharvalue);
			log.debug("[SimpleConnection::getConnection] Url: "+url);
			log.debug("[SimpleConnection::getConnection] Driver: "+driver);
			Class.forName(driver);
			Connection con = DriverManager.getConnection(url,p);
			alterSession(con);
			log.info("[SimpleConnection::getConnection] Return connection: "+con);
			log .info("[SimpleConnection::getConnection] END");
			return con;
		} catch (ClassNotFoundException e) {
			log.error("[SimpleConnection::getConnection] " + e.getMessage(),
					e);
			throw new DbException(e.getMessage());
		} catch (SQLException e) {
			log.error("[SimpleConnection::getConnection] " + e.getMessage(),
					e);
			throw new DbException(e.getMessage());
		}
	}

	public  ResourceBundle getBundle() {
		return bundle;
	}

	public DataSource getDataSource(String dbKeyName) throws DbException {
		final Connection con = getConnection(dbKeyName);
		DataSource ds = new DataSource(){
		
			public Connection getConnection() throws SQLException {
				// TODO Auto-generated method stub
				return con;
			}

			public Connection getConnection(String arg0, String arg1)
					throws SQLException {
				return null;
			}

			public PrintWriter getLogWriter() throws SQLException {
				// TODO Auto-generated method stub
				return new PrintWriter(System.out);
			}

			public int getLoginTimeout() throws SQLException {
				// TODO Auto-generated method stub
				return 0;
			}

			public void setLogWriter(PrintWriter arg0) throws SQLException {
				// TODO Auto-generated method stub
				
			}

			public void setLoginTimeout(int arg0) throws SQLException {
				// TODO Auto-generated method stub
				
			}
			
		};
		return ds;
	}

}
